/**
 * 
 */
package com.lgx.right.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import net.sf.json.JSONObject;

import com.lgx.common.entity.IdGenerator;
import com.lgx.common.web.util.DateUtils;
import com.lgx.common.web.util.DictUtils;

/**
 * @author hezhuo
 * 
 * 用户  or 会员
 *
 */
@Entity
@Table(name = "TS_USER")
public class User implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7248423154696569415L;

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator=IdGenerator.TS_USER_ID)
	@TableGenerator(
			name = IdGenerator.TS_USER_ID,
			table = IdGenerator.GENERATOR_TABLE,
			pkColumnName = IdGenerator.GEN_NAME,
			pkColumnValue = IdGenerator.TS_USER_ID,
			valueColumnName = IdGenerator.GEN_VALUE,
			initialValue = 1,
			allocationSize = 1
	)
	private long id;
	
	/**
	 * 登录用户名
	 */
	private String username;
	
	/**
	 * 登录密码
	 */
	private String password;
	
	/**
	 * 昵称
	 */
	private String nickname;
	
	/**
	 * 真实名称
	 */
	private String truename;
	
	/**
	 * 电子邮件,（目前可以为空）
	 */
	private String email;
	
	/**
	 * 性别
	 */
	private int sex = 0;
	
	/**
	 * 手机号码
	 */
	private String mobile;
	
	/**
	 * 对应会员卡
	 */
	@ManyToOne
	@JoinColumn(name = "card_id")
	private Card card;
	
	/**
	 * 父节点
	 */
	private long parent;
	
	/**
	 * 父节点类型
	 */
	private int parentType;
	
	/**
	 * 创建人
	 */
	@Column(name = "create_id")
	private long createId;
	
	private String createName;
	
	/**
	 * 用户类型，1普通用户，2联盟商家，3推广站，4推广中心，5运营中心，6公司员工
	 */
	private int userType;
	
	/**
	 * 所属组织结构ID
	 */
	@Column(name = "org_id")
	private long orgId;
	
	@SuppressWarnings("unused")
	private String orgName;
	
	private String orgType;
	
	/**
	 * 0 是 ，1否
	 */
	private String isMaster;
	
	/**
	 * 登录IP
	 */
	private String ip;
	
	/**
	 * 注册时间
	 */
	private Date registerTime;
	
	/**
	 * 最后一次登录时间
	 */
	private Date lastLogin;
	
	/**
	 * 是否可用
	 */
	private String isDelete;
	
	/**
	 * 用户锁定
	 */
	private Boolean locked;
	
	/**
	 * 会员卡号开始区间
	 */
	private String cardStart;
	
	/**
	 * 会员卡号结束区间
	 */
	private String cardEnd;
	
	/**
	 * 角色
	 */
	@ManyToMany(cascade={CascadeType.REFRESH},fetch=FetchType.EAGER)    
    @JoinTable(name="TS_USER_ROLE",   
             joinColumns=@JoinColumn(name="USER_ID"),   
             inverseJoinColumns=@JoinColumn(name="ROLE_ID")) 
	private Set<Role> roles;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getTruename() {
		return truename;
	}

	public void setTruename(String truename) {
		this.truename = truename;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public Card getCard() {
		return card;
	}

	public void setCard(Card card) {
		this.card = card;
	}

	public long getParent() {
		return parent;
	}

	public void setParent(long parent) {
		this.parent = parent;
	}

	public int getParentType() {
		return parentType;
	}

	public void setParentType(int parentType) {
		this.parentType = parentType;
	}

	public long getCreateId() {
		return createId;
	}

	public void setCreateId(long createId) {
		this.createId = createId;
	}

	public String getCreateName() {
		return createName;
	}

	public void setCreateName(String createName) {
		this.createName = createName;
	}

	public int getUserType() {
		return userType;
	}

	public void setUserType(int userType) {
		this.userType = userType;
	}

	public long getOrgId() {
		return orgId;
	}

	public void setOrgId(long orgId) {
		this.orgId = orgId;
	}

	public String getOrgName() {
		return DictUtils.getDictNameByKindAndCode(DictUtils.ORGANIZATION_TYPE, this.getOrgType());
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getOrgType() {
		return orgType;
	}

	public void setOrgType(String orgType) {
		this.orgType = orgType;
	}

	public String getIsMaster() {
		return isMaster;
	}

	public void setIsMaster(String isMaster) {
		this.isMaster = isMaster;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public Date getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public String getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(String isDelete) {
		this.isDelete = isDelete;
	}

	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	public String getCardStart() {
		return cardStart;
	}

	public void setCardStart(String cardStart) {
		this.cardStart = cardStart;
	}

	public String getCardEnd() {
		return cardEnd;
	}

	public void setCardEnd(String cardEnd) {
		this.cardEnd = cardEnd;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * 用户管理加载页面
	 * 会员管理加载页面
	 */
	@Override
	public String toString() {
		JSONObject obj = new JSONObject();
		
		obj.put("id", this.getId());
		obj.put("username", this.getUsername());   //登录名  
		obj.put("password", this.getPassword());
		obj.put("truename", this.getTruename());   //用户姓名
		obj.put("mobile", this.getMobile());
		obj.put("sex", this.getSex());
		obj.put("card", this.getCard().getId());   //卡号
		obj.put("score", this.getCard().getScore()); //卡的积分
		obj.put("registerTime", DateUtils.dateToChar(this.getRegisterTime(), DateUtils.DATE_FORMAT_D));
		obj.put("locked", this.getLocked());    //状态，是否锁定
		
		return obj.toString();
	}
}
